﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using System.Web.Mvc.Html;
using aspZone.Commerce.Model;
using System.Text;
using System.Configuration;

namespace aspZone.Commerce.Web.App.Helpers
{
    public static class GoogleCheckoutHelper
    {
        public static string GoogleCheckout(this HtmlHelper helper, Order order)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<form method=\"post\" action=\"{0}\">", FormAction);

            int index = 1;
            foreach (var item in order.Items)
            {

                sb.Append(helper.Hidden("item_name_" + index, helper.Encode(item.Product.Name)));
                sb.AppendFormat("<input type=\"hidden\" name=\"item_description_{0}\" value=\"{1}\">", index, helper.Encode(item.Product.Description));
                sb.AppendFormat("<input type=\"hidden\" name=\"item_price_{0}\" value=\"{1}\">", index, item.Product.Price);
                sb.AppendFormat("<input type=\"hidden\" name=\"item_currrency_{0}\" value=\"USD\">", index);
                sb.AppendFormat("<input type=\"hidden\" name=\"item_quantity_{0}\" value=\"{1}\">", index, item.Quantity);
                sb.AppendFormat("<input type=\"hidden\" name=\"item_merchant_id_{0}\" value=\"{1}\">", index, item.Product.Id);
                index++;
            }

            sb.Append(helper.Hidden("checkout-flow-support.merchant-checkout-flow-support.shipping-methods.flat-rate-shipping-1.name", "Standard"));
            sb.Append(helper.Hidden("checkout-flow-support.merchant-checkout-flow-support.shipping-methods.flat-rate-shipping-1.price", order.ShippingAndHandling));
            sb.Append(helper.Hidden("checkout-flow-support.merchant-checkout-flow-support.shipping-methods.flat-rate-shipping-1.price.currency", "USD"));
            sb.Append(helper.Hidden("checkout-flow-support.merchant-checkout-flow-support.shipping-methods.flat-rate-shipping-1.shipping-restrictions.allowed-areas.postal-area-1.country-code", "US"));
            sb.Append(helper.Hidden("checkout-flow-support.merchant-checkout-flow-support.shipping-methods.flat-rate-shipping-1.shipping-restrictions.allowed-areas.postal-area-2.country-code", "CA"));

            sb.AppendFormat("<input type=\"image\" name=\"Google Checkout\" alt=\"Fast checkout through Google\" src=\"{0}\" />", CheckoutImage);
            sb.Append("</form>");
            return sb.ToString();
        }

        private static string FormAction
        {
            get
            {
                if (Sandbox)
                {
                    return string.Format("https://sandbox.google.com/checkout/api/checkout/v2/checkoutForm/Merchant/{0}", MerchantId);
                }
                else
                {
                    return string.Format("https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/{0}", MerchantId);
                }
            }
        }

        private static string CheckoutImage
        {
            get
            {
                if (Sandbox)
                {
                    return string.Format("http://sandbox.google.com/checkout/buttons/checkout.gif?merchant_id={0}&w=180&h=46&style=white&variant=text&loc=en_US", MerchantId);
                }
                else
                {
                    return string.Format("https://checkout.google.com/buttons/checkout.gif?merchant_id={0}&w=180&h=46&style=white&variant=text&loc=en_US", MerchantId);
                }
            }
        }

        private static string MerchantId
        {
            get
            {
                return ConfigurationManager.AppSettings["Google.MerchantId"];
            }
        }

        private static bool Sandbox
        {
            get
            {
                bool sandbox = false;
                bool.TryParse(ConfigurationManager.AppSettings["Google.SandBoxMode"], out sandbox);
                return sandbox;
            }
        }
    }
}
